package com.banmao.snake_core.netty.message;

import cn.hutool.core.lang.UUID;
import com.banmao.snake_core.netty.message.impl.LoginRequestMessage;
import com.banmao.snake_core.netty.message.impl.LoginResponseMessage;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Data
public abstract class Message {

    private String sequenceId;

    public void setSequenceId(String sequenceId) {
        this.sequenceId = sequenceId;
    }

    public String getSequenceId() {
        if (StringUtils.isBlank(sequenceId)) {
            sequenceId = UUID.fastUUID().toString(true);
        }
        return sequenceId;
    }

    // 魔数 4字节
    public byte[] getMagicNum() {
        return "SNME".getBytes(StandardCharsets.UTF_8);
    }

    // 消息类型 4字节
    public abstract int getMessageType();

    // 消息类型
    protected static final int LoginRequestMessageType = 0; // 登陆请求
    protected static final int LoginResponseMessageType = 1; // 登陆响应

    static final Map<Integer, Class<?>> MESSAGETYPE_MAP = new ConcurrentHashMap<>();

    static {
        MESSAGETYPE_MAP.put(LoginRequestMessageType, LoginRequestMessage.class);
        MESSAGETYPE_MAP.put(LoginResponseMessageType, LoginResponseMessage.class);
    }

    public static Class<?> getMessageClass(int type) {
        return MESSAGETYPE_MAP.get(type);
    }

}
